﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using UserLicense.Applicationes.Command;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserLicense.Controllers
{
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserLoginController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IUserRepository userRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"></param>
        public UserLoginController( IMediator mediator, IUserRepository userRepository)
        {
            this._mediator = mediator;
            this.userRepository = userRepository;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost] 
        public async Task<int>AddUser(UserAddCommand userInfo)
        {
           return await _mediator.Send(userInfo);
        }

        /// <summary>
        /// 查询所有用户信息
        /// </summary>
        /// <param name="userList"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<UserDTO>>ShowUser(UserListCommand userList)
        {
            return await _mediator.Send(userList);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userLogin"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<string> UserLogin(UserLoginCommand userLogin)
        {
            return await  _mediator.Send(userLogin);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int>DeleteUser(int UserId)
        {
            return await userRepository.DeleteUser(UserId);
        }
    }
}
